#include "flash_emu.h"

#define FONT_SIZE 0x200000
#define FONT_BASE_ADDR 0x0800000

HANDLE hFontFile;
HANDLE hFontMapFile;
UINT8* lpFontMapAddress;

int font_emu_init()
{
	hFontFile = CreateFile("font.bin", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFontFile == INVALID_HANDLE_VALUE)
	{
		return 4;
	}
	hFontMapFile = CreateFileMapping(hFontFile, NULL, PAGE_READWRITE, 0, FONT_SIZE, NULL);
	if (hFontMapFile == NULL)
	{
		return 2;
	}
	lpFontMapAddress = (UINT8*)MapViewOfFile(hFontMapFile, FILE_MAP_ALL_ACCESS, 0, 0, FONT_SIZE);
	if (lpFontMapAddress == NULL)
	{
		return 3;
	}
	return 0x00;
}

void font_emu_release()
{
	BOOL bFlag = UnmapViewOfFile(lpFontMapAddress);
	bFlag = CloseHandle(hFontMapFile);
	bFlag = CloseHandle(hFontFile);
}

UINT8 font_emu_read(UINT32 addr)
{
	return lpFontMapAddress[(addr & 0x00FFFFFF) - FONT_BASE_ADDR];
}

#undef FONT_SIZE
#undef FONT_BASE_ADDR